package core;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;

public class HtmlServices {

    public static String saveImage(URL url, String path, String filename) throws IOException {
        filename = filename.replaceAll("[\\'?<>/\\\\]", "");
        File f = new File(path + filename);
        if (f.exists()) return filename;
        System.out.println("Download image: " + url + "[" + filename + "]");

        // if image not exists --> download it
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.addRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*");
        connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5");
        connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
        connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; MS-RTC LM 8)");
        connection.connect();
        InputStream is = connection.getInputStream();
        OutputStream os = new FileOutputStream(path + filename);

        byte[] buffer = new byte[1024];
        int byteReaded = is.read(buffer);
        while (byteReaded != -1) {
            os.write(buffer, 0, byteReaded);
            byteReaded = is.read(buffer);
        }

        os.close();
        return filename;
    }

    public static void resize(String file, int max_width, int max_height, String newFile) {
        int new_height = -1;
        int new_width = -1;

        try {
            System.out.println(file);
            BufferedImage picture = ImageIO.read(new File(file));
            //System.out.println(picture.getWidth() +" / "+max_width+" > "+picture.getHeight()+" / "+max_height);

            if ((float)picture.getWidth() / max_width > (float)picture.getHeight() / max_height) {
                new_width = max_width; 
            }
            else {
                new_height = max_height; //135
                
            }
            Image newImage = picture.getScaledInstance(new_width, new_height, Image.SCALE_SMOOTH);

            BufferedImage image = new BufferedImage(newImage.getWidth(null), newImage.getHeight(null), BufferedImage.TYPE_INT_RGB);

            Graphics2D g = image.createGraphics();
            g.drawImage(newImage, 0, 0, null);

            ImageIO.write(image, "JPG", new File(newFile));

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println(file);
            e.printStackTrace();
        }
    }
    

}
